<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>axios入门与源码解析</title>
  <link rel="stylesheet" href="../bootstrap.min.css">
  <script src="../axios.js"></script>
</head>

<body>
  <div class="container">
    <h2>axios基本使用</h2>
    <p>
      <a href="https://github.com/axios/axios">去Git学习axios</a>
    </p>
    <button class="btn btn-primary">发送GET请求</button>
    <button class="btn btn-warning">发送POST请求</button>
    <button class="btn btn-success">发送PUT请求</button>
    <button class="btn btn-danger">发送DELETE请求</button>
  </div>
  <script>
    // 获取按钮
    const btns = document.querySelectorAll('button')
    // 默认配置
    axios.defaults.method='GET'
    axios.defaults.baseURL='http://localhost:3000'
    axios.defaults.params={id:100}
    axios.defaults.timeout=3000
    // 第一个
    btns[0].onclick = function () {
      axios.request({
        url: '/comments',
      }).then(response => {
        console.log(response)
      })
    }
    // 第二个
    btns[1].onclick = function () {
      axios.post(
        'http://localhost:3000/comments',
        {
         body:'喜欢你，没道理！！！',
         postId:2
        }).then(res=>{
          console.log(res)
        })
    }
    // 第三个
    btns[2].onclick = function () {
      axios({
        // 请求类型
        method: 'PUT',
        // URL
        url: 'http://localhost:3000/posts/4',
        // 设置请求体
        data: {
          title: 'PUT更新' + Math.random(),
          author: "尚硅谷"
        }
      }).then(response => {
        console.log(response)
      })
    }
    // 第四个
    btns[3].onclick = function () {
      axios({
        // 请求类型
        method: 'DELETE',
        // URL
        url: 'http://localhost:3000/posts/3',
      }).then(response => {
        console.log(response)
      })
    }
  </script>
</body>

</html>